package com.example.mathutil;

import java.util.Random;

/**
 * 工具类
 * @author SJ
 *
 */
public class JuyixiaUtil {
    // 将字符串转换成二进制字符串，以空格相隔  
    public static String StrToBinstr(String str) {  
        char[] strChar = str.toCharArray();  
        String result = "";  
        for (int i = 0; i < strChar.length; i++) {  
            result += Integer.toBinaryString(strChar[i]) + " ";  
        }  
        return result;  
    }  
    // 将二进制字符串转换成Unicode字符串  
    public static String BinstrToStr(String binStr) {  
        String[] tempStr = StrToStrArray(binStr);  
        char[] tempChar = new char[tempStr.length];  
        for (int i = 0; i < tempStr.length; i++) {  
            tempChar[i] = BinstrToChar(tempStr[i]);  
        }  
        return String.valueOf(tempChar);  
    }  
  
    // 将二进制字符串格式化成全16位带空格的Binstr  
    public String BinstrToBinstr16(String input) {  
        StringBuffer output = new StringBuffer();  
        String[] tempStr = StrToStrArray(input);  
        for (int i = 0; i < tempStr.length; i++) {  
            for (int j = 16 - tempStr[i].length(); j > 0; j--)  
                output.append('0');  
            output.append(tempStr[i] + " ");  
        }  
        return output.toString();  
    }  
  
    // 将全16位带空格的Binstr转化成去0前缀的带空格Binstr  
    public String Binstr16ToBinstr(String input) {  
        StringBuffer output = new StringBuffer();  
        String[] tempStr = StrToStrArray(input);  
        for (int i = 0; i < tempStr.length; i++) {  
            for (int j = 0; j < 16; j++) {  
                if (tempStr[i].charAt(j) == '1') {  
                    output.append(tempStr[i].substring(j) + " ");  
                    break;  
                }  
                if (j == 15 && tempStr[i].charAt(j) == '0')  
                    output.append("0" + " ");  
            }  
        }  
        return output.toString();  
    }  
    // 将二进制字符串转换为char  
    public static char BinstrToChar(String binStr) {  
        int[] temp = BinstrToIntArray(binStr);  
        int sum = 0;  
        for (int i = 0; i < temp.length; i++) {  
            sum += temp[temp.length - 1 - i] << i;  
        }  
        return (char) sum;  
    }  
  
    // 将初始二进制字符串转换成字符串数组，以空格相隔  
    private static String[] StrToStrArray(String str) {  
        return str.split(" ");  
    }  
  
    // 将二进制字符串转换成int数组  
    public static int[] BinstrToIntArray(String binStr) {  
        char[] temp = binStr.toCharArray();  
        int[] result = new int[temp.length];  
        for (int i = 0; i < temp.length; i++) {  
            result[i] = temp[i] - 48;  
        }  
        return result;  
    } 
    /**
     * 自定义MAC()加密算法
     */
    public static String MAC(String message,String key){
   	 char[] charArray = message.toCharArray();  
   	 long k=Long.parseLong(key);
        for(int i =0;i<charArray.length;i++){  
            charArray[i]=(char)(charArray[i]^k);  
        }    
        return String.valueOf(charArray);  
   
   }
    public static String RandomNum17(){
    	Random r=new Random();
    	int a=r.nextInt(99999999);
    	int b=r.nextInt(99999999);
    	long l=a*1000000000L+b; 
    	String num=String.valueOf(l);
    	return num;
    }
    public static String RandomNum(){
    	Random r=new Random();
    	int a=r.nextInt(99999999);
    	int b=r.nextInt(99999999);
    	long l=a*100000000000000000L+b; 
    	String num=String.valueOf(l);
    	return num;
    }
    public  static String getEncryption(String para,String r1,String r2,String r3){  
        Long a=Long.parseLong(r1);
        Long a1=Long.parseLong(r2);
        Long a2=Long.parseLong(r3);
        
         char[] charArray = para.toCharArray();  
         for(int i =0;i<charArray.length;i++){  
             charArray[i]=(char)(charArray[i]^a^a1^a2);  
         }    
         return String.valueOf(charArray);  
     }  
    public  static String xor(String para,String r1,String r2,String r3){  
        Long a=Long.parseLong(r1);
        Long a1=Long.parseLong(r2);
        Long a2=Long.parseLong(r3);
         char[] charArray = para.toCharArray();  
         for(int i =0;i<charArray.length;i++){  
             charArray[i]=(char)(charArray[i]^a^a1^a2);  
         }    
         return String.valueOf(charArray);  
     }
}
